﻿namespace Hidistro.UI.Web.Admin
{
    using Hidistro.ControlPanel.Store;
    using Hidistro.Entities;
    using Hidistro.UI.ControlPanel.utility;
    using Ionic.Zip;
    using System;
    using System.IO;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Xml;

    public class IosUpgrade : AdminPage
    {
        protected Button btnUpoad;
        protected FileUpload fileUpload;
        protected HtmlInputHidden hidIsForcibleUpgrade;
        protected Literal litDescription;
        protected Literal litUpgradeUrl;
        protected Literal litVersion;

        private void btnUpoad_Click(object sender, EventArgs e)
        {
            if (!this.fileUpload.HasFile)
            {
                this.ShowMsg("请先选择一个数据包文件", false);
            }
            else if ((this.fileUpload.PostedFile.ContentLength == 0) || (((this.fileUpload.PostedFile.ContentType != "application/x-zip-compressed") && (this.fileUpload.PostedFile.ContentType != "application/zip")) && (this.fileUpload.PostedFile.ContentType != "application/octet-stream")))
            {
                this.ShowMsg("请上传正确的数据包文件", false);
            }
            else
            {
                string str = this.Page.Request.MapPath("~/storage/data/app/ios");
                string fileName = Path.GetFileName(this.fileUpload.PostedFile.FileName);
                string filename = Path.Combine(str, fileName);
                this.fileUpload.PostedFile.SaveAs(filename);
                DirectoryInfo info = new DirectoryInfo(str);
                using (ZipFile file = ZipFile.Read(Path.Combine(info.FullName, fileName)))
                {
                    foreach (ZipEntry entry in file)
                    {
                        entry.Extract(info.FullName, ExtractExistingFileAction.OverwriteSilently);
                    }
                }
                File.Delete(filename);
                this.LoadVersion();
                AppVersionRecordInfo latestAppVersionRecord = APPHelper.GetLatestAppVersionRecord("ios");
                if (latestAppVersionRecord == null)
                {
                    latestAppVersionRecord = new AppVersionRecordInfo {
                        Device = "ios",
                        Version = 0.00M
                    };
                }
                decimal result = 1.00M;
                decimal.TryParse(this.litVersion.Text, out result);
                if (result > latestAppVersionRecord.Version)
                {
                    bool flag = false;
                    bool.TryParse(this.hidIsForcibleUpgrade.Value, out flag);
                    latestAppVersionRecord.Version = result;
                    latestAppVersionRecord.IsForcibleUpgrade = flag;
                    latestAppVersionRecord.Description = this.litDescription.Text;
                    latestAppVersionRecord.UpgradeUrl = this.litUpgradeUrl.Text;
                    APPHelper.AddAppVersionRecord(latestAppVersionRecord);
                }
                this.ShowMsg("上传成功！", true);
            }
        }

        private void LoadVersion()
        {
            XmlDocument document = new XmlDocument();
            try
            {
                document.Load(HttpContext.Current.Request.MapPath("/storage/data/app/ios/IosUpgrade.xml"));
                this.litVersion.Text = document.SelectSingleNode("root/Version").InnerText;
                this.litDescription.Text = document.SelectSingleNode("root/Description").InnerText;
                this.litUpgradeUrl.Text = document.SelectSingleNode("root/UpgradeUrl").InnerText;
            }
            catch
            {
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnUpoad.Click += new EventHandler(this.btnUpoad_Click);
            if (!this.Page.IsPostBack)
            {
                this.LoadVersion();
            }
        }
    }
}

